/**
 * 抽奖轮盘
 * 状态枚举 status ['pause 未开始','start 开始','running 抽奖中', 'end 活动结束']
 */
import moment from 'moment';
import {
  getRouletteStatus,
  timeListener,
  randomInt,
} from './index';
import {
  getUserAwardInfo,
  awardQualification,
} from '/common/api';

class Roulette {
  constructor() {
    // 每场活动持续时间, 单位m, 默认值5天
    this.duration = 5 * 24 * 60;
    // 抽奖轮盘组件对象
    this.roulette = {};
    // 当前状态
    this.status = 'pause';
    // 下一次活动开始时间
    this.nextTime = 0;
    // 当前活动时间
    this.activityTime = 0;
  }

  // 抽奖轮盘初始化
  init(roulette) {
    this.roulette = roulette;
    getRouletteStatus(this.duration)
      .then((res) => {
        const {
          current,
          status,
          nextTime,
          activityTime,
        } = res;
        this.nextTime = nextTime;
        this.activityTime = moment(activityTime).format('YYYY-MM-DD H:mm:ss');
        this.currentTime = current;
        roulette.setData({
          nextTimeDate: moment(nextTime).format('MM月DD日 HH:mm'),
        });
        switch(status) {
          case 'pause': 
            this.pauseStatus(status, current, nextTime);
            break;
          case 'start':
            this.startStatus(status, current, nextTime);
            break;
          case 'end':
            this.endStatus(status);
            break;
          default: break;
        }
      }).catch((err) => {
        console.log(err);
      });
  }

  // 未开始状态
  pauseStatus(status, current, nextTime) {
    this.status = status;
    // 倒计时
    timeListener(this.refresh.bind(this), current, nextTime);
    this.roulette.setData({
      status,
      nextTime: moment(nextTime).format('HH:mm'),
      activeIndex: 0,
    });
  }

  // 开始状态
  startStatus(status, current, nextTime) {
    awardQualification(this.activityTime)
      .then((res) => {
        const { data } = res;
        const { success } = data;
        if (success) {
          this.status = status;
          this.roulette.setData({
            status,
            activeIndex: 0,
          });
        } else {
          this.pauseStatus('pause', current, nextTime);
        }
      }).catch((err) => {
        console.log(err);
      });
  }

  // 抽奖中
  runningStatus() {
    this.status = 'running';
    this.roulette.setData({
      status: 'running',
    });
  }

  // 活动结束
  endStatus(status) {
    this.status = status;
    this.roulette.setData({
      status,
      activeIndex: 0,
    });
  }

  // 当前点击事件
  eventListener() {
    const $this = this;
    if (this.status === 'start') {
      this.runningStatus();
      let result = -1,
          activeIndex = 0,
          speed = 150,
          resDec = '',
          turns = 0;
      let timer = setInterval(() => {
        $this.roulette.setData({
          activeIndex: ++activeIndex
        });
        if (activeIndex === result && turns > 2) {
          clearInterval(timer);
          $this.roulette.handleShowResult(resDec);
        }
        if (activeIndex >= 8) {
          activeIndex = 0;
          turns++;
        }
      }, speed);
      // 请求中奖结果信息
      getUserAwardInfo(this.activityTime)
        .then((res) => {
          const { data } = res;
          if (data.success) {
            resDec = data.data;
            const przeList = ['一等奖', '二等奖', '三等奖', '四等奖'];
            const przeIndex = przeList.findIndex((value) => {
              return value === data.data.awardDesc;
            });
            result = 2 * przeIndex + 1;
          } else {
            const rs = [2, 4, 6, 8];
            result = rs[randomInt(0, 3)];
          }
        }).catch((err) => {
          console.log(err);
        });
    }
  }

  // 刷新状态
  refresh() {
    if (this.roulette) {
      this.roulette.setData({
        activeIndex: 0,
      });
      this.init(this.roulette);
    }
  }
}

export default new Roulette();